package com.session;

import org.apache.commons.collections4.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class GroupChatServerContextImpl implements GroupChatServerContext {

    private final Map<String, Set<String>> GROUP_NAME_MAPPING_GROUP_MEMBERS_MAP = new ConcurrentHashMap<>();

    @Override
    public void createGroupChat(String groupName, List<String> groupMembers) {

        this.createGroupChat(groupName,new HashSet<>(groupMembers));

    }

    @Override
    public void createGroupChat(String groupName, Set<String> groupMembers) {
        this.GROUP_NAME_MAPPING_GROUP_MEMBERS_MAP.put(groupName,groupMembers);
    }

    @Override
    public Set<String> getGroupMembersByGroupName(String groupName) {

        return this.GROUP_NAME_MAPPING_GROUP_MEMBERS_MAP.getOrDefault(groupName,new HashSet<>());
    }

    @Override
    public void exitChatGroup(String groupName, List<String> exitMemberNames) {
        Set<String> groupMembersSet = this.GROUP_NAME_MAPPING_GROUP_MEMBERS_MAP.get(groupName);
        if(CollectionUtils.isEmpty(groupMembersSet)){
            return;
        }
        exitMemberNames.forEach(groupMembersSet::remove);
    }
}
